require 'tk'

root = TkRoot.new(title:"hello scale")

$var_score = TkVariable.new(0)
$vars = Array.new(3){TkVariable.new(0)}

%w[Food Ambiance Service].each_with_index do |x,i|
	 TkScale.new{
		label x
		variable $vars[i]
		length "10c"
		width "0.25c"
		from 0
		to 5
		resolution 1
		tickinterval 1
		showvalue 1
		orient "horizontal"
		command {
			update_score
		}
		grid padx:2,pady:2,sticky:"w"
	}
end

lab_score = TkLabel.new{
	textvariable $var_score
	grid padx:2,pady:2,sticky:"w"
}

def update_score
	vars = $vars
	total = vars.inject(0) {|sum,x| sum+=x.value.to_i}
	average = total / 3.0
	$var_score.value = "Average score : %3.1f" % [average]
end

Tk.mainloop